package com.yy.utils;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import cn.hutool.http.Method;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSONObject;


/**
 * @Description:
 * @ClassName: HutoolUtil
 * @author: 姚欣宏
 * @date: 2023.07.10
 */
public class HutoolUtil {
    public static HttpResponse sendPost(String url, String token, JSONObject json) {
        HttpRequest request = HttpUtil.createRequest(Method.POST, url);
        request.header("Connection", "keep-alive")
                .header("Origin", "http://guahao.wchscu.cn")
                .header("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X -1_0_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36")
                .header("token", token)
                .header("Content-Type", "application/json;charset=UTF-8")
                .header("Accept", "*/*")
                .header("Referer", "http://guahao.wchscu.cn/file/h5/index.html")
                .header("Accept-Encoding", "gzip, deflate")
                .header("Accept-Language", "zh-CN,zh;q=0.9")
                .header("Cookie", "acw_tc=781bad2316886240948188586e511e2b80c89998fb3a2a62e2492d387dfbde")
                .body(json.toString());
        return request.execute();
    }
    public static HttpResponse sendGet(String url, String token) {
        HttpRequest request = HttpUtil.createRequest(Method.GET, url);
        request.header("Connection", "keep-alive")
                .header("Origin", "http://guahao.wchscu.cn")
                .header("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X -1_0_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36")
                .header("token", token)
                .header("Accept", "*/*")
                .header("Referer", "http://guahao.wchscu.cn/file/h5/index.html")
                .header("Accept-Encoding", "gzip, deflate")
                .header("Accept-Language", "zh-CN,zh;q=0.9")
                .header("Cookie", "acw_tc=781bad2316886240948188586e511e2b80c89998fb3a2a62e2492d387dfbde");
        return request.execute();
    }


    public static <T> T parseJsonResponse(HttpResponse response, Class<T> clazz) {
        String body = response.body();
        if (response.isOk()) {
            return JSONUtil.toBean(body, clazz);
        } else {
            throw new RuntimeException("请求失败，状态码：" + response.getStatus());
        }
    }
}
